<?php
final class Logger {
	const LOG_FILE_NAME = 'system.log';
	const DATE_TIME_FORMAT = "Y-m-d\tH:i:s";
	const MESSAGE_FORMAT = "%s\n%s\n%s\n\n";
	const ERROR_LEVEL = 0;
	const WARNING_LEVEL = 1;
	const INFORMATION_LEVEL = 2;
	const DEBUG_LEVEL = 3;

	private static $levels = array('ERROR', 'WARNING', 'INFORMATION', 'DEBUG');
	private static $level = self::ERROR_LEVEL;

	public static function setLevel($level) {
		self::$level = (int)$level;
	}

	public static function error($message) {
		self::write($message, self::ERROR_LEVEL);
	}

	public static function warning($message) {
		self::write($message, self::WARNING_LEVEL);
	}

	public static function info($message) {
		self::write($message, self::INFORMATION_LEVEL);
	}

	public static function debug($message) {
		self::write($message, self::DEBUG_LEVEL);
	}

	private static function write($message, $level) {
		if($level <= self::$level) {
			$level = self::$levels[$level];
			$date = new DateTime(NULL,new DateTimeZone(date_default_timezone_get()));
			$date = $date->format(self::DATE_TIME_FORMAT);
			$file = fopen(self::LOG_FILE_NAME, 'a');
			fprintf($file, self::MESSAGE_FORMAT, $level, $date, $message);
			fclose($file);
		}
	}
}
?>